package com.books.controller.shoppingcar;/*
 *@Auther Zhourui
 *@data 2021/9/26  8:25
 *@Email 1605576647@qq.com
 */

import com.books.model.bo.Result;
import com.books.model.entity.book.Books;
import com.books.model.entity.book.Ebook;
import com.books.model.entity.common.People;
import com.books.model.entity.login_regis.UserLogin;
import com.books.model.entity.shopping.ShoppingCar;
import com.books.service.book.IBookService;
import com.books.service.book.IEbookService;
import com.books.service.people.IPeopleService;
import com.books.service.shoppingcar.IShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/car" ,method = RequestMethod.GET)
public class ShoppingCarController {
    @Autowired(required = false)
    private IBookService bookService;

    @Autowired(required = false)
    private IEbookService ebookService;

    @Autowired(required = false)
    private IPeopleService peopleService;

    @Autowired(required = false)
    private ShoppingCar shoppingCar;

    @Autowired(required = false)
    private IShoppingCarService shoppingCarService;

    @GetMapping("/shoppingcar")
    public String shoppingcar(HttpServletRequest servletRequest, String account) {
        Integer uid = peopleService.getPeopleByAccount(account).getPeopleId();
        List<ShoppingCar> shoppingCarList = shoppingCarService.findAllByUid(uid);
        servletRequest.setAttribute("shoppingcarlist", shoppingCarList);
        return "shoppingcar/shoppingcar";
    }

    @GetMapping("/add")
    @ResponseBody
    public Result addcar(HttpServletRequest request, String Id) {
        HttpSession session = request.getSession();
        Books book = bookService.getById(Integer.valueOf(Id));
        UserLogin user = (UserLogin) session.getAttribute("user");
        if (user == null) {
            return new Result("500", "请先登陆！");
        } else {
            String account = user.getAccount();
            People people = peopleService.getPeopleByAccount(account);
            if (shoppingCarService.getByBid(Integer.valueOf(Id)) != null) {
                shoppingCar = shoppingCarService.getByBid(Integer.valueOf(Id));
                shoppingCar.setNum(shoppingCar.getNum() + 1);
                shoppingCar.setTotal(shoppingCar.getPrice()*shoppingCar.getNum());
                shoppingCarService.updataCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            } else {
                shoppingCar.setBookId(book.getBkId());
                shoppingCar.setImgUrl(book.getPictureUrl());
                shoppingCar.setName(book.getTitle());
                shoppingCar.setPrice(book.getPrice());
                shoppingCar.setNum(1);
                shoppingCar.setTotal(shoppingCar.getNum() * shoppingCar.getPrice());
                shoppingCar.setRemark("1");
                shoppingCar.setUserId(people.getPeopleId());
                shoppingCar.setBookType(1);
                String num = String.valueOf(shoppingCar.getBookId()) + String.valueOf(shoppingCar.getUserId());
                shoppingCar.setId(Integer.valueOf(num));
                shoppingCarService.addCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            }
        }
    }

    @GetMapping("/addMore")
    @ResponseBody
    public Result addMore(HttpServletRequest request, String Id,String buyNum) {
        HttpSession session = request.getSession();
        Books book = bookService.getById(Integer.valueOf(Id));
        UserLogin user = (UserLogin) session.getAttribute("user");
        if (user == null) {
            return new Result("500", "请先登陆！");
        } else {
            String account = user.getAccount();
            People people = peopleService.getPeopleByAccount(account);
            if (shoppingCarService.getByBid(Integer.valueOf(Id)) != null) {
                shoppingCar = shoppingCarService.getByBid(Integer.valueOf(Id));
                shoppingCar.setNum(shoppingCar.getNum() + Integer.valueOf(buyNum));
                shoppingCar.setTotal(shoppingCar.getPrice()*shoppingCar.getNum());
                shoppingCarService.updataCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            } else {
                shoppingCar.setBookId(book.getBkId());
                shoppingCar.setImgUrl(book.getPictureUrl());
                shoppingCar.setName(book.getTitle());
                shoppingCar.setPrice(book.getPrice());
                shoppingCar.setNum(Integer.valueOf(buyNum));
                shoppingCar.setTotal(shoppingCar.getNum() * shoppingCar.getPrice());
                shoppingCar.setRemark("1");
                shoppingCar.setUserId(people.getPeopleId());
                shoppingCar.setBookType(1);
                String num = String.valueOf(shoppingCar.getBookId()) + String.valueOf(shoppingCar.getUserId());
                shoppingCar.setId(Integer.valueOf(num));
                shoppingCarService.addCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            }
        }
    }
    @GetMapping("/update")
    @ResponseBody
    public Result updatecar(HttpServletRequest request, Integer cid, Integer number) {
        HttpSession session = request.getSession();
        shoppingCar = shoppingCarService.findById(cid);
        shoppingCar.setNum(number);
        shoppingCar.setTotal(shoppingCar.getPrice() * number);
        shoppingCarService.updataCar(shoppingCar);
        return new Result("200", "更新成功");
    }

    @GetMapping("/del")
    @ResponseBody
    public Result updatecar(HttpServletRequest request, Integer cid) {
        int n = shoppingCarService.delCarById(cid);
        if (n != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("500", "删除失败");
    }

    @GetMapping("/adde")
    @ResponseBody
    public Result addcare(HttpServletRequest request, String Id) {
        HttpSession session = request.getSession();
        Ebook book = ebookService.getById(Integer.valueOf(Id));
        UserLogin user = (UserLogin) session.getAttribute("user");
        if (user == null) {
            return new Result("500", "请先登陆");
        } else {
            String account = user.getAccount();
            People people = peopleService.getPeopleByAccount(account);
            if (shoppingCarService.getByBid(Integer.valueOf(Id)) != null) {
                shoppingCar = shoppingCarService.getByBid(Integer.valueOf(Id));
                shoppingCar.setNum(shoppingCar.getNum() + 1);
                shoppingCarService.updataCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            } else {
                shoppingCar.setBookId(book.getEbkId());
                shoppingCar.setImgUrl(book.getPictureUrl());
                shoppingCar.setName(book.getTitle());
                shoppingCar.setPrice(book.getPrice());
                shoppingCar.setNum(1);
                shoppingCar.setTotal(shoppingCar.getNum() * shoppingCar.getPrice());
                shoppingCar.setRemark("0");
                shoppingCar.setUserId(people.getPeopleId());
                shoppingCar.setBookType(2);
                String num = String.valueOf(shoppingCar.getBookId()) + String.valueOf(shoppingCar.getUserId());
                shoppingCar.setId(Integer.valueOf(num) + 100000);
                shoppingCarService.addCar(shoppingCar);
                return new Result("200", "添加购物车成功");
            }
        }
    }
}